﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SideviewSceneScript : MonoBehaviour {

	Animator birdAnim;
	SpriteRenderer birdRend;
	JoyStickScript joyScript;
	float sensitivity = 100f;
	AudioSource mainMusic;
	AudioSource storyMusic;
	bool fireButtonPressed = false;
	SpriteRenderer projectile;
	//public List<TextMeshAnimationCommand> TextAnimationList;
	List<TextMesh> storyTexts = new List<TextMesh>();
	float startTime = 0f;
	float endTime = 10f;
	List<TextMeshAnimationCommand> TextAnimationList = new List<TextMeshAnimationCommand>();

	Animator backAnim;
	SpriteRenderer backRend;

	// Use this for initialization
	void Start () {
		this.backAnim = (Animator)GameObject.Find ("Back").GetComponent (typeof(Animator));
		this.backRend = (SpriteRenderer)GameObject.Find ("Back").GetComponent (typeof(SpriteRenderer));
		
		this.backAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.BackUp);

		this.birdAnim = (Animator)GameObject.Find("Hurburd").GetComponent(typeof(Animator));
		this.birdRend = (SpriteRenderer)GameObject.Find("Hurburd").GetComponent(typeof(SpriteRenderer));
		this.birdAnim.SetBool("IsFlapping",true);
		this.joyScript = (JoyStickScript)GameObject.Find("Scripts").GetComponent(typeof(JoyStickScript));
		this.joyScript.FireButtonPressedEvent += delegate(object sender, System.EventArgs e) {
			this.fireButtonPressed = true;
		};
		this.projectile = (SpriteRenderer)GameObject.Find("hurpern").GetComponent(typeof(SpriteRenderer));

		this.storyMusic = (AudioSource)GameObject.Find("Irradiance").GetComponent(typeof(AudioSource));

		if (PlayerPrefs.HasKey ("MusicVolume")) {
			int MusicVolume = PlayerPrefs.GetInt("MusicVolume");
			this.storyMusic.volume = Mathf.Lerp(0.0f,0.5f,(float)MusicVolume / 10.0f);
		}
		else {
			this.storyMusic.volume = 0.5f;
		}

		this.mainMusic = (AudioSource)GameObject.Find("MainMusic").GetComponent(typeof(AudioSource));
		this.mainMusic.Pause();

		// One of the times I do things statically instead of dynamically
		// My apologies to the coder, this is not good coding practice IMO.
		// I wanted to get the game out in a reasonable amount of time
		// so I will revisit later a way to either work within Unity's
		// Animator system entirely or do a more generic scripted way,
		// which you can see with IAnimationCommand in part.
		float timeAccumulated = 2f;
		for (int z=1;z<=14;z++) {
			TextMesh mesh = (TextMesh)GameObject.Find("Text"+z).GetComponent(typeof(TextMesh));
			mesh.color = new Color(0f,0f,0f,0f);

			TextMeshAnimationCommand animCommand = new TextMeshAnimationCommand(mesh.transform);
			//animCommand.AnimationObject = mesh.transform;
			Vector3 botPos = mesh.transform.position;
			Vector3 topPos = mesh.transform.position;
			botPos.y -= 5f;
			topPos.y += 5f;


			animCommand.TransformFromPosition = botPos;
			animCommand.TransformEndPosition = mesh.transform.position;
			animCommand.LocalScaleFromPosition = mesh.transform.localScale * 8f;
			animCommand.LocalScaleEndPosition = mesh.transform.localScale;
			animCommand.ColorFromValue = new Color(0f,0f,0f,0f);
			animCommand.ColorEndValue = new Color(1f,1f,1f,1f);
			animCommand.StartTime = timeAccumulated;
			timeAccumulated += 1f;
			animCommand.EndTime = timeAccumulated;
			this.TextAnimationList.Add(animCommand);

			animCommand = new TextMeshAnimationCommand(mesh.transform);
			//animCommand.AnimationObject = mesh.transform;
			animCommand.TransformFromPosition = mesh.transform.position;
			animCommand.TransformEndPosition = mesh.transform.position;
			animCommand.LocalScaleFromPosition = mesh.transform.localScale;
			animCommand.LocalScaleEndPosition = mesh.transform.localScale;
			animCommand.ColorFromValue = new Color(1f,1f,1f,1f);
			animCommand.ColorEndValue = new Color(1f,1f,1f,1f);
			animCommand.StartTime = timeAccumulated;
			timeAccumulated += 8f;
			animCommand.EndTime = timeAccumulated;
			this.TextAnimationList.Add(animCommand);

			animCommand = new TextMeshAnimationCommand(mesh.transform);
			//animCommand.AnimationObject = mesh.transform;
			animCommand.TransformFromPosition = mesh.transform.position;
			animCommand.TransformEndPosition = topPos;
			animCommand.LocalScaleFromPosition = mesh.transform.localScale;
			animCommand.LocalScaleEndPosition = mesh.transform.localScale / 8f;
			animCommand.ColorFromValue = new Color(1f,1f,1f,1f);
			animCommand.ColorEndValue = new Color(0f,0f,0f,0f);
			animCommand.StartTime = timeAccumulated;
			timeAccumulated += 1f;
			animCommand.EndTime = timeAccumulated;
			this.TextAnimationList.Add(animCommand);

			botPos.y -= 20f;
			mesh.transform.position = botPos;

		}
		timeAccumulated += 2f;
		endTime = timeAccumulated;
	}

	void FixedUpdate() {
		if (this.joyScript.DistanceMoved != 0f) {
			//Vector2 pos = this.birdRend.transform.position;
			//pos.x += this.joyScript.XMovement * sensitivity;
			//pos.y += this.joyScript.YMovement * sensitivity;
			//this.birdRend.transform.position = pos;
			this.birdRend.rigidbody2D.AddForce(new Vector2(this.joyScript.XMovement * sensitivity, this.joyScript.YMovement * sensitivity));

			Vector3 scale = this.birdRend.transform.localScale;
			if (this.joyScript.XMovement < 0f) {
				//scale.x = -1.5f;
			}
			else {
				scale.x = 1f;
			}
			this.birdRend.transform.localScale = scale;
		}

		/*
		if (this.fireButtonPressed) {
			GameObject newObject = (GameObject)GameObject.Instantiate(this.projectile.gameObject, this.birdRend.transform.position, Quaternion.identity);
			ProjectileScript script = (ProjectileScript)newObject.GetComponent(typeof(ProjectileScript));
			script.Started = true;
			newObject.rigidbody2D.gravityScale = 0f;
			newObject.rigidbody2D.AddForce(new Vector2(400f,0f));
			this.fireButtonPressed = false;
		}
		*/
	}
	
	// Update is called once per frame
	void Update () {

		bool inBack = false;
		bool inBackInitially = false;

		inBack = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.Pos);
		inBackInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.ClickDownPos);
		if (GameMaintainerSingleton.Instance.CheckButton(
			inBack,inBackInitially,
			this.backRend,this.backAnim,
			MenuButtonConstants.MenuConstants.BackDown,MenuButtonConstants.MenuConstants.BackUp,
			MenuButtonConstants.MenuTriggerString)) {
			
			this.mainMusic.Play();
			Application.LoadLevel(0);
		}

		if (Input.GetKeyUp(KeyCode.Escape)) {
			this.mainMusic.Play();
			Application.LoadLevel(0);
		}
		startTime += Time.deltaTime;
		//Debug.Log(startTime);
		foreach(TextMeshAnimationCommand cmd in this.TextAnimationList) {
			cmd.SetCurrentFrame(startTime);
		}
		if (startTime >= endTime) {
			this.mainMusic.Play();
			Application.LoadLevel(0);
		}
	}
}
